package com.zzjson.activemq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2019 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : com.zzjson.activemq</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2019年11月19日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
public class JMSQueueListenerReceiver {
	public static void main(String[] args) {
		ConnectionFactory mqConnectionFactory = new ActiveMQConnectionFactory("tcp://116.62.221.6:61616");
		Connection connection = null;
		try {
			connection = mqConnectionFactory.createConnection();

			connection.start();
			Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
			//创建目的地
			Destination destination = session.createQueue("myQueue");
			//创建接受者
			MessageConsumer consumer = session.createConsumer(destination);

			MessageListener messageListener = new MessageListener() {

				@Override
				public void onMessage(Message message) {
					try {
						System.out.println(((TextMessage) message).getText());
					} catch (JMSException e) {
						e.printStackTrace();
					}
				}
			};
			while (true) {
				consumer.setMessageListener(messageListener);
//创建需要发送的消息
				session.commit();
				TextMessage receive = (TextMessage) consumer.receive();

			}

		} catch (JMSException e) {
			e.printStackTrace();
		} finally {
			if (connection != null) {
				try {
					connection.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}

			}
		}
	}
}